<template>
	<view>
		<view class="flex align-start justify-start solid-bottom" style="width: 100%;">
			<view class="padding flex align-center justify-start" style="flex-direction: column;">
				<view @tap="jumpToUserProfile(comment.author.user_id)" class="cu-avatar round"
					:style="'background-image:url('+comment.author.avatar+')'">
					<image v-if="comment.author.sex!==-1" class="sex-icon round" :src="'../static/images/setting/'+[comment.author.sex==0?'female':'male']+'.png'"></image>
				</view>
				<view v-if="comment.author.role!==null" class="padding-top-xs">
					<view style="max-width: 65upx;" :class="'radius cu-tag sm bg-'+roleColor">
						{{comment.author.role.name}}
					</view>
				</view>
			</view>
			<view class="flex justify-start align-start padding-right padding-top"
				style="flex-direction: column;width: 100%;">
				<view class="flex justify-between align-end padding-bottom-sm" style="width: 100%;">
					<view class="flex align-center justify-start">
						<view class="flex justify-start align-start" style="flex-direction: column;">
							<view class="flex justify-start align-center">
								<view v-if="showPostOwnerTag&&comment.owner_id==comment.post_owner_id"
									class="padding-right-xs">
									<view class="cu-tag line-blue sm" style="max-height: 26upx;">楼主</view>
								</view>
								<text @tap="jumpToUserProfile(comment.author.user_id)"
									class="text-grey text-sm">{{comment.author.nickname}}</text>
								<view v-if="comment.author.group!==null"
									class="flex justify-start align-center padding-left-xs">
									<view class="radius cu-tag sm bg-gray text-grey">{{comment.author.group.name}}
									</view>
								</view>
							</view>
							<text class="text-gray text-sm">{{comment.show_time}}</text>
						</view>
					</view>
					<view class="flex justify-end align-center">
						<view v-if="isManager&&comment&&comment.audit_status!== -1" class="action"
							@tap="lockCommentAction(comment)">
							<text class="cuIcon-lock text-gray"></text>
						</view>
						<view v-else class="action" @tap="reportCommentAction(comment)">
							<text class="cuIcon-warn text-gray"></text>
						</view>
					</view>
				</view>
				<view v-if="comment&&comment.audit_status==-1"
					:class="(comment.parent_comment!==null||(comment.parent_comment_id&&checkParentDelete))?'padding-top-xs':''">
					<text class="text-grey" style="letter-spacing: 1.5upx;">内容涉嫌违规已被击飞</text>
				</view>
				<view @longpress.stop="copyContent(comment.content)" v-else-if="comment&&comment.content&&comment.content.length > 0"
					:class="(comment.parent_comment!==null||(comment.parent_comment_id&&checkParentDelete))?'padding-top-xs':''">
					<rich-text v-if="comment&&comment.content&&comment.content.length < 80" :selectable="true"
						:user-select="true" class="comment-text" :nodes="comment.content.replace(reg, emotion)">
					</rich-text>
					<view @tap.stop="expandComment" v-else-if="comment.is_expand==true"
						class="flex justify-start align-start" style="flex-direction: column;">
						<rich-text :selectable="true" :user-select="true"
							class="comment-text" :nodes="comment.content.replace(reg, emotion)"></rich-text>
						<text class="action text-blue text-sm">收起</text>
					</view>
					<view v-else class="flex flex-wrap">
						<rich-text @tap.stop="expandComment" :selectable="true"
							:user-select="true" class="comment-text"
							:nodes="comment.content.slice(0,79).replace(reg, emotion)">
						</rich-text>
						<text @tap.stop="expandComment" v-if="hideExpandBtn==false"
							class="action text-blue text-sm">展开</text>
					</view>
				</view>
				<view v-if="comment && comment.audit_status !==-1 && comment.image_list && comment.image_list.length>0"
					style="width: 100%;" :class="'padding-top-'+[comment.content&&comment.content.length>0?'xs':'sm']">
					<view :style="'max-width:'+(comment.image_list.length>2?'80%;':'50%;')"
						:class="'grid flex-sub grid-square col-'+comment.image_list.length">
						<view class="bg-img" v-for="(url,index) in comment.image_list" :key="index" @tap="ViewImage"
							:data-list="comment.image_list" :data-url="url">
							<image :src="url+'?imageView2/2/w/500/h/500/q/75'" mode="aspectFill"></image>
						</view>
					</view>
				</view>
				<!-- @提到某人 -->
				<view v-if="comment&&comment.at_user_list&&comment.at_user_list.length>0"
					class="flex justify-start padding-top-sm" style="flex-wrap: wrap;">
					<view v-for="(item,index) in  comment.at_user_list" :key="index">
						<view class="padding-left-sm padding-top-sm">
							<view @tap="jumpToUserProfile(item.author.user_id)" class="round cu-tag text-blue"
								style="background-color: #eff6fb;">
								@{{item.author.nickname}}
							</view>
						</view>
					</view>
				</view>
				<!-- 最近三条回复 -->
				<view v-if="comment&&comment.reply_list&&comment.reply_list.length>0"
					class="bg-gray radius padding-xs margin-top-xs flex justify-start align-start"
					style="flex-direction: column;width: 100%;">
					<view v-for="(replyItem,replyIndex) in comment.reply_list" :key="replyIndex"
						class="flex justify-start align-start" style="flex-direction: column;width: 100%;">
						<view  style="width: 100%;" :class="replyIndex>0?'padding-top-xs':''">
							<view v-if="replyItem.content&&replyItem.content.length>0&&replyItem.content.length<66&&comment.reply_list.length>1">
								<rich-text @tap.stop="jumpToCommentDetail(replyItem)" class="text-gray text-df reply-text"  :nodes="combineContent(replyItem)"></rich-text>
							</view>
							<view @tap.stop="jumpToCommentDetail(replyItem)"
								v-else-if="replyItem.content&&comment.reply_list.length==1&&replyItem.content.length<80&&replyItem.content.length>0">
								<rich-text @tap.stop="jumpToCommentDetail(replyItem)" class="text-gray text-df reply-text"  :nodes="combineContent(replyItem)"></rich-text>
							</view>
							<view @tap.stop="jumpToCommentDetail(replyItem)"
								v-else-if="replyItem.content&&comment.reply_list.length==1&&replyItem.content.length>=80&&replyItem.content.length>0">
								<rich-text @tap.stop="jumpToCommentDetail(replyItem)" class="text-gray text-df reply-text"  :nodes="combineContent(replyItem,65)"></rich-text>
							</view>
							<view @tap.stop="jumpToCommentDetail(replyItem)"
								v-else-if="replyItem.content&&replyItem.content.length>0">
								<rich-text @tap.stop="jumpToCommentDetail(replyItem)" class="text-gray text-df reply-text"  :nodes="combineContent(replyItem,50)"></rich-text>
							</view>
							<text @tap.stop="jumpToCommentDetail(replyItem)"
								v-else-if="replyItem.content==null||replyItem.content.length==0"
								class="text-blue text-df">{{replyItem.author.nickname}}:<text class="text-gray text-df"
									style="color: #888;letter-spacing: 1.5upx;">{{replyItem.audit_status==-1?'内容涉嫌违规已被击飞':'[图片]'}}</text></text>
							<view
								v-if="comment.reply_list.length==1&&replyItem.image_list.length>0&&replyItem.audit_status==1">
								<text @tap.stop="expandImage" v-if="comment.is_image_expand==false">...<text
										class="text-blue text-sm">展开</text></text>
								<view v-else style="width: 100%;">
									<view :style="'max-width:'+(replyItem.image_list.length>2?'80%;':'25%;')"
										:class="'grid flex-sub grid-square col-'+replyItem.image_list.length">
										<view class="bg-img" v-for="(url,imgIndex) in replyItem.image_list"
											:key="imgIndex" @click.stop="ViewImage" :data-list="replyItem.image_list"
											:data-url="url">
											<image :src="url+'?imageView2/2/w/500/h/500/q/75'" mode="aspectFill">
											</image>
										</view>
									</view>
									<text @tap.stop="expandImage" class="action text-blue text-sm">收起</text>
								</view>
							</view>
						</view>
						<view
							:class="'flex justify-end align-center '+[replyIndex!==comment.reply_list.length-1?'solid-bottom':'']"
							style="width: 100%;">
							<view @tap.stop="replyInnerPraiseAction(replyIndex)"
								v-if="replyItem && replyItem.audit_status !== -1"
								:class="comment.reply_list.length>0&&replyIndex!==comment.reply_list.length-1?'padding-bottom-xs':''">
								<view class="flex justify-end align-center text-df action">
									<text class="cuIcon-appreciate "
										:style="'color:'+[replyItem.is_praise?'#e54d42;':'#bbb;']"></text>
									<text
										:style="'color:'+[replyItem.is_praise?'#e54d42;':'#bbb;']">{{replyItem.praise_count>0?replyItem.praise_count:''}}</text>
								</view>
							</view>
							<view v-else class="padding-bottom-xs"></view>
							<view @tap.stop="jumpToCommentDetail(replyItem)" v-if="replyItem.reply_count>0"
								:class="'padding-left-xl '+[comment.reply_list.length>0&&replyIndex!==comment.reply_list.length-1?'padding-bottom-xs':'']">
								<view class="flex justify-end align-center action text-df">
									<text class="cuIcon-comment" style="color: #bbb;"></text>
									<text style="color: #bbb;">{{replyItem.reply_count}}</text>
								</view>
							</view>
							<view v-else class="padding-bottom-xs"></view>
							<view @tap.stop="replyInnerCommentAction(replyItem)"
								v-if="replyItem&&replyItem.audit_status !== -1"
								:class="'padding-left-xl '+[comment.reply_list.length>0&&replyIndex!==comment.reply_list.length-1?'padding-bottom-xs':'']">
								<view class="flex justify-end align-center action text-df">
									<text class="cuIcon-community" style="color: #bbb;"></text>
								</view>
							</view>
							<view v-else class="padding-bottom-xs"></view>
						</view>
					</view>
					<text @tap.stop="detailAction" v-if="comment.reply_count>3" class="text-blue text-sm padding-top-xs"
						style="width: 100%;">查看全部{{comment.reply_count}}条回复</text>
				</view>
				<view class="flex justify-between align-center" style="width: 100%;">
					<view class="flex justify-start align-center" style="width: 80%;">
						<view v-if="comment && comment.audit_status !== -1"
							class="padding-left-xs padding-top-sm padding-bottom-sm">
							<view @tap="praiseAction(comment,index,false)"
								class="flex justify-end align-center padding-right-xl action">
								<text :class="'cuIcon-appreciate text-'+[comment.is_praise?'red':'gray']"></text>
								<text :class="'text-'+[comment.is_praise?'red':'gray']">点赞({{comment.praise_count}})</text>
							</view>
						</view>
						<view v-else class="padding-bottom-sm"></view>
						<view v-if="hideReplyCountBtn==false&&comment.reply_count>0"
							class="padding-top-sm padding-bottom-sm">
							<view class="flex justify-end align-center action" @tap="detailAction">
								<text class="cuIcon-comment text-gray"></text>
								<text class="text-gray">评论 ({{comment.reply_count}})</text>
							</view>
						</view>
					</view>
					<view v-if="comment&&comment.audit_status !== -1"
						class="padding-left-xs padding-top-sm padding-bottom-sm">
						<view class="flex justify-end align-center action" @tap="replyCommentAction(comment)">
							<text class="cuIcon-community text-gray"></text>
							<text class="text-gray">回复</text>
						</view>
					</view>
					<view v-else class="padding-bottom-sm"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getTagColorForRole
	} from '@/api/utils.js'
	import {
		checkLogin
	} from '@/js_sdk/checkLogin.js'
	import {
		getReportTipReadStatus,
		setReportTipFinishRead,
		isLogin,
		getUserInfo
	} from '@/api/auth.js'
	export default {
		name: "CommentItem",
		data() {
			return {
				reg: /\#[\S]{1,3}\;/gi
			};
		},
		computed: {
			isManager() {
				if (isLogin() == false) {
					return false
				}
				let userInfo = getUserInfo()
				if (userInfo.role == null) {
					return false
				}
				return userInfo.role_id > 0
			},
			roleColor() {
				if (this.comment == null ||
					this.comment.author == null ||
					this.comment.author.role == null) {
					return 'white'
				}
				return getTagColorForRole(this.comment.author.role.role_id)
			}
		},
		props: {
			comment: {
				type: Object,
				default: {}
			},
			checkParentDelete: {
				type: Boolean,
				default: true
			},
			showPostOwnerTag: {
				type: Boolean,
				default: true
			},
			hideReplyCountBtn: {
				type: Boolean,
				default: false
			},
			hideExpandBtn: {
				type: Boolean,
				default: false
			},
			index: {
				type: Number,
				default: -1
			},
			isHot: {
				type: Boolean,
				default: false
			}
		},
		methods: {
			combineContent(comment,subLength) 
			{
				let content = "<span class='text-df text-blue'>"+comment.author.nickname+":</span>"
				let commentContent = subLength&&subLength.length>0? comment.content.slice(0,subLength)+'...':comment.content
				content += comment.audit_status==-1?'内容涉嫌违规已被击飞':commentContent.replace(this.reg,this.emotion) 
			    let imageText =	comment.image_list&&comment.image_list.length>0?'[图片]':''
				return content+imageText
			},
			copyContent(content) {
				uni.setClipboardData({
					data: content,
					success: function() {
						uni.showToast({
							icon: 'none',
							title: '内容已复制!'
						});
					}
				});
			},
			replyEmotion(res) {
				//console.log('mess', res)
				let word = res.replace(/\#|\;/gi, '')
				//console.log(word)
				const list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过',
					'酷',
					'冷汗', '抓狂', '吐', '偷笑', '可爱', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂', '疑问', '嘘',
					'晕', '折磨', '衰',
					'骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲',
					'吓', '可怜',
					'菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀',
					'足球', '瓢虫',
					'便便', '月亮', '太阳', '礼物', '拥抱', '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情',
					'飞吻', '跳跳',
					'发抖', '怄火', '转圈', '磕头', '回头', '跳绳', '挥手', '激动', '街舞', '献吻', '左太极', '右太极'
				]
				let index = list.indexOf(word)
				return `<img src="https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/${index}.gif" style="vertical-align:middle;" width="14" height="14">`
			},
			emotion(res) {
				//console.log('mess', res)
				let word = res.replace(/\#|\;/gi, '')
				//console.log(word)
				const list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过',
					'酷',
					'冷汗', '抓狂', '吐', '偷笑', '可爱', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂', '疑问', '嘘',
					'晕', '折磨', '衰',
					'骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲',
					'吓', '可怜',
					'菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀',
					'足球', '瓢虫',
					'便便', '月亮', '太阳', '礼物', '拥抱', '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情',
					'飞吻', '跳跳',
					'发抖', '怄火', '转圈', '磕头', '回头', '跳绳', '挥手', '激动', '街舞', '献吻', '左太极', '右太极'
				]
				let index = list.indexOf(word)
				return `<img src="https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/${index}.gif" style="vertical-align:middle;" width="20" height="20">`
			},
			replyInnerPraiseAction(innerIndex) {
				if (isLogin() == false) {
					checkLogin()
					return
				}
				console.log('reply praise innnerIndex ' + innerIndex)
				this.$emit('replyPraise', [this.index, innerIndex, this.isHot])
			},
			replyInnerCommentAction(comment) {
				if (isLogin() == false) {
					checkLogin()
					return
				}
				uni.navigateTo({
					url: '/pages/comment/comment?postId=' + comment.post_id,
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						res.eventChannel.emit('replyComment', comment)
					}
				})
			},
			jumpToCommentDetail(comment) {
				uni.navigateTo({
					url: '/pages/always/comment-detail/comment-detail?commentId=' + comment.comment_id
				})
			},
			expandImage() {
				this.$emit('expandImageAction')
			},
			expandComment() {
				this.$emit('expandAction')
			},
			lockCommentAction(comment) {
				this.$emit('lock')
			},
			jumpToUserProfile(userId) {
				uni.navigateTo({
					url: '/pages/profile/profile?userId=' + userId
				})
			},
			deleteAction() {
				this.$emit('delete')
			},
			replyCommentAction() {
				this.$emit('reply')
			},
			reportCommentAction() {
				if (isLogin() == false) {
					checkLogin()
					return
				}
				let status = getReportTipReadStatus()
				if (status == false) {
					let that = this
					uni.showModal({
						title: '平台提示',
						content: '请如实根据当前需举报内容的真实性进行举报，如果多次举报被判定无效举报或者恶意举报将会被平台永久拉黑!',
						confirmText: '知道了',
						cancelText: '手滑了',
						success(res) {
							if (res.confirm) {
								that.$emit('report')
							}
						},
						complete() {
							setReportTipFinishRead()
						}
					})
				} else {
					this.$emit('report')
				}
			},
			praiseAction() {
				this.$emit('praise')
			},
			detailAction() {
				this.$emit('detail')
			},
			detailParentAction() {
				this.$emit('detailParent')
			},
			ViewImage(e) {
				console.log(e.currentTarget.dataset.list)
				console.log(e.currentTarget.dataset.url)
				uni.previewImage({
					urls: e.currentTarget.dataset.list,
					current: e.currentTarget.dataset.url
				});
			},
		},
	}
</script>

<style lang="scss">
	.comment-text {
		font-size: 30upx;
		color: #333333;
		letter-spacing: 1.5upx;
	}
	.reply-text {
		color: #888;
		letter-spacing: 1.5upx;
	}
	.sex-icon {
		z-index: 10;
		position: absolute;
		bottom: -3px;
		width: 20upx;
		height: 20upx;
		background-color: #FFF;
	}
</style>
